package com.zmn.plat.business.impl.channel;

import org.apache.dubbo.config.annotation.Reference;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.business.interfaces.channel.ChannelSpBService;

import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.channel.sp.ChannelSp;
import com.zmn.plat.services.interfaces.channel.sp.ChannelSpService;
import com.zmn.sp.common.model.manage.SpServProviderDRO;
import com.zmn.sp.dubbo.interfaces.manage.SpManageListRemoteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 描述: 渠道管理新增服务商配置
 *
 * @author jiafei
 * @version 1.0
 * @since 2021/6/10 9:36
 */
@Slf4j
@Service
public class ChannelSpBServiceImpl implements ChannelSpBService {

    @Resource
    private ChannelSpService channelSpService;

    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private SpManageListRemoteService spManageListRemoteService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseDTO<List<Integer>> add(Integer channelId, List<Integer> spIds, Integer type, String operator) {

        try {
            // 查询是否已经存在名单之中
            List<ChannelSp> channelSps = channelSpService.listByChannelIdAndSpIds(channelId, spIds);
            if (CollectionUtil.isNotNullOrEmpty(channelSps)) {
                ChannelSp channelSp = channelSps.get(0);
                String msg = Objects.equals(channelSp.getType(), 1) ? "黑名单" : "白名单";
                return ResponseDTO.fail(String.format("服务商已存在%s中，先移除后再操作", msg));
            }
            // 查询服务商
            ResponseDTO<List<SpServProviderDRO>> responseDTO = spManageListRemoteService.listServProviderByIds(spIds);
            if (Objects.isNull(responseDTO) || !responseDTO.isSuccess()) {
                return ResponseDTO.fail("无效的服务商数据");
            }
            // 保存
            List<ChannelSp> channelSpList = new ArrayList<>();
            Date now = DateUtil.getNow();
            responseDTO.getData().forEach(spDRO -> {
                ChannelSp sp = new ChannelSp();
                sp.setChannelId(channelId);
                sp.setSpId(spDRO.getSpId());
                sp.setSpName(spDRO.getName());
                sp.setCompanyName(spDRO.getFullName());
                sp.setSpTwoType(spDRO.getTwoType());
                sp.setSpThirdType(spDRO.getThirdType());
                sp.setType(type);
                sp.setPlat(spDRO.getPlat());
                sp.setCreater(operator);
                sp.setCreateTime(now);
                sp.setUpdater(operator);
                sp.setUpdateTime(now);
                channelSpList.add(sp);
            });

            channelSpService.insertBatch(channelSpList);

            return ResponseDTO.success(spIds, "操作成功！");
        } catch (Exception e) {
            log.error("新增/修改失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }
}
